#include "MessageFactory.h"

MessageFactory* MessageFactory::pFactory_ = NULL;

MessageFactory::MessageFactory()
{
    
}

MessageFactory::~MessageFactory()
{
    clearAllMessage();
}

void MessageFactory::clearAllMessage()
{
    std::map<TMessageID, IMessageCreator*>::iterator it;
    for(it = messageCreatorMap_.begin();
        it != messageCreatorMap_.end(); it++)
    {
        delete it->second;
    }
    
    messageCreatorMap_.clear();
}

google::protobuf::MessageLite* MessageFactory::createMessageByID(TMessageID msgID)
{
    std::map<TMessageID, IMessageCreator*>::iterator it;
    it = messageCreatorMap_.find(msgID);
    if(it != messageCreatorMap_.end())
    {
        return it->second->create();
    }
    else
    {
        // @todo: add log: message not found.
        printf("MessageID missed: %d.\n", msgID);
    }
    
    return NULL;
}
